package com.blog.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.user.entity.Blog;
import com.blog.user.entity.vo.UserBlog;
import com.blog.user.mapper.ArticleMapper;
import com.blog.user.mapper.ArticleTagMapper;
import com.blog.user.mapper.BlogMapper;
import com.blog.user.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zw
 * @since 2022-02-21
 */
@SuppressWarnings("all")
@Slf4j
@Service
@Transactional
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Autowired
    BlogMapper blogMapper;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    ArticleTagMapper articleTagMapper;
    @Override
    public Map<String, Object> getAllBlog(Integer pageSize, Integer limit) {
        pageSize=(pageSize-1)*limit;
        List<Blog> userBlogs = blogMapper.getAllBlog(pageSize,limit);
        Map<String,Object> map=new HashMap<>();
        if(ObjectUtils.isEmpty(userBlogs))
            return null;
        List<UserBlog> list=new ArrayList<>();
        Integer blogCount = blogMapper.getAllBlogCount();
        map.put("blogCount", blogCount);
        userBlogs.forEach(blog -> {
            UserBlog userBlog=new UserBlog(
                    blog.getUserId(),
                    blog.getBlogTitle(),
                    blog.getBlogViews(),
                    blog.getBlogCollections(),
                    blog.getArticleId(),
                    blog.getBlogTime(),
                    blog.getLastTime(),
                    blog.getBlogType());
            list.add(userBlog);
        });
        map.put("userBlog", list);
        return map;
    }

    @Override
    public Map<String, Object> getAllBlogByTitle(String title, Integer pageSize, Integer limit) {
            pageSize=(pageSize-1)*limit;
            List<Blog> userBlogs = blogMapper.getAllBlogByTitle(title,pageSize,limit);
            int count=0;
            Map<String,Object> map=new HashMap<>();
            if(ObjectUtils.isEmpty(userBlogs))
                return null;
            List<UserBlog> list=new ArrayList<>();
            Integer blogCount = blogMapper.getAllBlogByTitleCount(title);
            map.put("blogCount", blogCount);
            userBlogs.forEach(blog -> {
                UserBlog userBlog=new UserBlog(
                        blog.getUserId(),
                        blog.getBlogTitle(),
                        blog.getBlogViews(),
                        blog.getBlogCollections(),
                        blog.getArticleId(),
                        blog.getBlogTime(),
                        blog.getLastTime(),
                        blog.getBlogType());
                list.add(userBlog);
            });
            map.put("userBlog", list);
            return map;
    }

    @Override
    public boolean deleteArticle(String userId, Integer id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("article_id", id);
        articleMapper.deleteByAttach(id);
        articleMapper.deleteComment(id);
        int delete = blogMapper.delete(queryWrapper);
        int delete1 = articleTagMapper.delete(queryWrapper);
        int delete2 = articleMapper.deleteById(id);
        if(delete>0 && delete1>0 && delete2>0)
            return true;
        return false;
    }
}
